package com.codeye.spider.siteName;

import java.sql.SQLException;
import java.util.List;

import com.codeye.audio.AudioFilePlayer;
import com.codeye.office.excel.ExcelReadTools;
import com.codeye.office.excel.ExcelWriteTools;
import com.codeye.spider.Queue;

public class SN_index {
	//直接连网站 获取页面上网站的名称即可
	public static void main(String[] args) throws SQLException {
//		String filePath = "d:/test2.xls";
		String filePath = "d:/特殊产品域名清单（客户版本）.xls";
		
		
		
		boolean isHasTitle = false; 	//文档是否有表头
		int keyColNum = 1;				//第几列是网站地址,真实列数
		int equalTimes = 0;				//结果完整性检测，数字远大结果越完整，需要的时间也会更长，一次5秒
		
		List<String> urlList = ExcelReadTools.Read_sm(filePath, isHasTitle, keyColNum);//最后一个数字参数，代表第几列是域名
		
		for(String url : urlList){
			Queue.addElem(url.trim());
		}
		
		Spider_sn[] spiderCage = new Spider_sn[50];
		for (int i = 0; i < spiderCage.length; i++) {
			spiderCage[i] = new Spider_sn(i);
			
			new Thread(spiderCage[i]).start();
		}
		
		boolean writeFlag = false;
		AudioFilePlayer ap = new AudioFilePlayer();
		String sepath = System.getProperty("user.dir")+"\\src\\com\\codeye\\audio\\se\\";
		int oldResultCount = 0;
		while (!writeFlag) {
			if(!writeFlag && Queue.isEmpty()){
				int newResultCount = Queue.size_resultmap();
				if(newResultCount == oldResultCount){
					equalTimes += 1;
				}else{
					equalTimes = 0;
					oldResultCount = Queue.size_resultmap();
				}
				if(equalTimes > 3){// || retryTimes >= coherenceCheckTimes
					ap.play(sepath + "Reserch complete.wav");
					System.out.println("*************************************************************  开始写文件"+filePath+"    *****************");
					ExcelWriteTools.Write_rw(filePath, Queue.getRmap(), false, keyColNum, 0);	
					System.out.println("*************************************************************  结束写文件"+filePath+"    *****************");
					ap.play(sepath + "Upgrade complete.wav");
					ap.play(sepath + "job finish.wav");
					System.exit(0);
				}else{
					ap.play(sepath + "not enough energy.wav");
				}
			}

			try {
				System.out.println("*****************************************************************  任务总量【"+urlList.size()+"】     **************");
				System.out.println("*****************************************************************  队列剩余【"+Queue.size()+"】     **************");
				System.out.println("*****************************************************************  完成总量【"+Queue.size_resultmap()+"】     **************");
				System.out.println("*****************************************************************  重试次数【"+equalTimes+"】     **************");
				
				Thread.sleep(5000); // 暂停，每一秒输出一次
			} catch (InterruptedException e) {
				return;
			}
		}
	}
}
